package com.example.task;

import com.example.config.FinalConfig;
import com.example.model.SdInfo;
import com.example.service.DeviceService;
import com.example.service.ITableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class DeviceTask {

    @Autowired
    ITableService tableService;

    @Autowired
    DeviceService deviceService;

    @Scheduled(cron = "0 * * * * ?")
    public void queryNetWorkDevice(){
        log.info("拉取网络设备定时开始！");
        tableService.updateBySql("INSERT INTO sd_info ( device_name, device_type,device_model, device_id, mac_address, communication_mode) select device_name,device_type,device_model,device_id,REPLACE(device_number,'-',':'),communication_mode from `data-collection-server`.dc_collection_device where communication_mode =1 and delete_flag =0 and effective_status=1   ON DUPLICATE KEY UPDATE device_name=`data-collection-server`.dc_collection_device.device_name,device_type=`data-collection-server`.dc_collection_device.device_type");
    }


    @Scheduled(cron = "0 0/30 * * * ?")
    //@Scheduled(cron = "0 * * * * ?")
    public void updateDeviceInfo(){
        log.info("网络设备状态巡查开始！");
        SdInfo sdInfo=new SdInfo();
        sdInfo.setIsDelete(FinalConfig.DELETE_NO);
        List<SdInfo> sdInfos = deviceService.selectList(sdInfo);
        sdInfos.forEach( info->{
            if(info.getIpAddress()!=null && !"".equals(info.getIpAddress())){
                deviceService.refreshDevice(info);
            }
        });
    }

}
